import {
  deleteCard,
  reset,
  level,
  save,
  page
} from '@/services/MemCardService';


const MemCardModel = {
  namespace: 'memCard',
  state: {
    saved: {},
    page: {content: [],totalElements:0},
    refresh: false
  },
  effects: {
    * page({payload}, {call, put}) {
      const response = yield call(page, payload);
      yield put({
        type: 'pageRes',
        payload: response,
      });
    },
    * save({payload}, {call, put}) {
      const response = yield call(save, payload);
      yield put({
        type: 'editRes',
        payload: response,
      });
    },
    * level({payload}, {call, put}) {
      const response = yield call(level, payload);
      yield put({
        type: 'editRes',
        payload: response,
      });
    },
    * reset({payload}, {call, put}) {
      const response = yield call(reset, payload);
      yield put({
        type: 'editRes',
        payload: response,
      });
    },
    * deleteCard({payload}, {call, put}) {
      const response = yield call(deleteCard, payload);
      yield put({
        type: 'refreshRes',
        payload: response,
      });
    },
  },
  reducers: {
    editRes(state, {payload}) {
      let result={...state};
      result.saved=payload;
      result.refresh=!state.refresh;
      return result;
    },
    refreshRes(state, {payload}) {
      let result={...state};
      result.refresh=!state.refresh;
      return result;
    },
    pageRes(state, {payload}) {
      let result={...state};
      result.page=payload;
      return result;
    }
  },
}
export default MemCardModel;
